//
//  Player.m
//  SWEngiOS
//
//  Created by Eric Augustine on 3/26/12.
//  Copyright 2012 UW - Parkside. All rights reserved.
//

#import "Player.h"
#import "Game.h"
#import "Bet.h"
#import "Guess.h"


@implementation Player

@synthesize playerName;
@synthesize currAnswer;
@synthesize permChips;
@synthesize gameChips;
@synthesize currScore;
@synthesize currBet;
@synthesize chipsBet;
@synthesize betRatio;
@synthesize game;

- (id) initWithName: (NSString*)n {
	if(self = [super init]){ 
		self.playerName=n;
		self.permChips=2;
		self.gameChips=0;
		self.chipsBet=0;
		self.currScore=0;
		self.currBet=0;
		self.betRatio=2.0;
	} 
	return self; 
}

- (id) initWithName: (NSString*)n andPermChips: (NSInteger)pChips{
	if(self = [super init]){ 
		self.playerName=n;
		self.permChips=pChips;
		self.gameChips=0;
		self.chipsBet=0;
		self.currScore=0;
		self.currBet=0;
		self.betRatio=2.0;
	} 
	return self; 
}

- (void) placeBet: (NSInteger)choice :(NSInteger)chips{
	NSInteger totalChips = gameChips + permChips;
	if(chips > totalChips){
		chips = totalChips;
	}
	if(chips > totalChips - chipsBet){
		chips = totalChips - chipsBet;
	}
	Bet* bet = [[Bet alloc] initWithPlayerName:playerName andChipAmt:chips andChoice:choice];
	[game receiveBets:bet];
	chipsBet += chips;
}

- (NSString*) joinGame: (Game*)g{
	if([g addPlayer:self]){
		game = g;
		return [NSString stringWithFormat:@"%@ has been added to game %@", playerName, g.name];
	}else{
		return [NSString stringWithFormat:@"%@ was not added to the game. %@ is currently full. Please try a different game.", playerName, g.name];
	}
}

- (void) submitGuess: (NSInteger)numGuess{
	Guess* guess = [[Guess alloc] initWithPlayerName:playerName andGuess:numGuess];
	[game receiveGuess:guess];
}

- (void) printChips{
	NSLog(@"Player Name : %@", self.playerName);
	NSLog(@"Permanent Chips : %@", self.permChips);
	NSLog(@"Game Chips : %@", self.gameChips);
}

@end
